package com.myspring.lab.filter;

import javax.servlet.*;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class filter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String user = (String) request.getSession().getAttribute("user");
        String url = request.getRequestURI();
        if(url.contains("login")||url.contains("regist")||url.contains("images")||url.contains("css")||url.contains("js")){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }
        if(user==null){
            response.sendRedirect(request.getContextPath() + "/login.html");
            return;
        }
        if(url.contains("user")){
            if(user.equals("root")||user.equals("admin")){
                filterChain.doFilter(servletRequest,servletResponse);
                return;
            }else{
                response.sendRedirect(request.getContextPath()+"/eee");
            }
        }
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}